#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_custom_node.py 
@version:
@time: 2022/05/25 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM
import random

x_list = random.sample('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.lower(), 11)
x = ''.join(x_list)


class TMFCustomNode:

    # 创建
    @staticmethod
    def _post_custom_node(payload=None):
        if payload is None:
            payload = {}
        _payload = {
            "name": "robot_mobile",
            "formSchema": {"form": {"labelCol": 6, "wrapperCol": 12, "style": {"opacity": 1}},
                           "schema": {"type": "object", "properties": {
                               "field_name": {"type": "string", "title": "修改字段名", "x-decorator": "FormItem",
                                              "x-component": "Input",
                                              "x-component-props": {"style": {"opacity": 1}},
                                              "x-decorator-props": {"style": {"opacity": 1}},
                                              "x-designable-id": x, "x-index": 0,
                                              "name": "ield_name"}},
                                      "x-designable-id": x}},
            "template": """function process(record, form)
            {\n\nvar str=\"18912341234\"
            \nvar pat=/(\\d{3})\\d*(\\d{4})/
            \nvar b=str.replace(pat,'$1****$2');
            \nconsole.log(b)
            \nrecord[form.field_name] =  record[form.field_name].replace(\"1234\",\"****\");
            \nreturn record;\n}\n""",
            # "template": "functionprocess(record, form)
            # {\n\nvar str=\"18912341234\"\nvar pat=/(\\d{3})\\d*(\\d{4})/\nvar b=str.replace(pat,'$1****$2');
            # \nconsole.log(b);\nrecord[form.field_name] =  record[form.field_name].replace(\"1234\",\"****\");
            # \nreturn record;\n}\n"
            # "template": "functionprocess(record, form)
            # {\n\nvar str=\"18912341234\"\nvar pat=/(\\d{3})\\d*(\\d{4})/\nvar b=str.replace(pat,'$1****$2');
            # \nconsole.log(b);\nrecord[form.field_name]=\"123\";
            # \nreturn record;\n}\n"

        }
        _payload.update(payload)
        return TM.post_custom_node(_payload).get('data')

    # 删除
    @staticmethod
    def _delete_custom_node_by_id(_id):
        return TM.delete_custom_node_by_id(_id)

    # 查询
    @staticmethod
    def _get_custom_by_filter(_filter):
        # {"where": {}, "order": "last_updated DESC", "limit": 20, "skip": 0}
        return TM.get_custom_node_by_filter(_filter).get('data')

    # 修改
    @staticmethod
    def _patch_custom_node(_id, payload):
        new_payload = {
            'id': _id,
            **payload
        }
        return TM.patch_custom_node(new_payload).get('data')

    def get_custom_node_by_name(self, name):
        limit = 20
        skip = 0
        while True:
            _filter = {
                "order": "createTime DESC",
                "limit": limit,
                "skip": skip,
                "where": {}
            }
            response = self._get_custom_by_filter(_filter)
            items = response.get('items')
            if not items:
                break
            else:
                for one in items:
                    if one.get('name') == name:
                        return one
            skip += limit

    def delete_custom_node_by_name(self, name):
        result = self.get_custom_node_by_name(name)
        if result:
            self._delete_custom_node_by_id(result.get('id'))

    def create_custom_node(self, payload=None):
        return self._post_custom_node(payload)

    def patch_custom_node_by_name(self, name, payload):
        result = self.get_custom_node_by_name(name)
        result.update(payload)
        return self._patch_custom_node(result.get('id'), payload)


if __name__ == '__main__':
    T = TMFCustomNode()
    res = T.patch_custom_node_by_name('robot_mobile', {'name': 'robot_mobile1'})
    print(res)
    res = T.patch_custom_node_by_name('robot_mobile1', {'name': 'robot_mobile'})
    print(res)
